<?php
class Courses extends MY_Controller {
    
    function __construct() {
        parent::__construct();
        if ( !$this->ion_auth->logged_in() ) {
            redirect('/auth');
        }
    }
 
    public function index() {
    }
    
    /**
     * 
     */
    public function get($id = NULL) {
        $course = new Course();
        $course->where('id', $id)
                ->where_related_user('id', $this->ion_auth->user()->row()->id)
                ->include_related('category')
                ->include_related('classroom')
                ->get();
        $attachment = new Attachment();
        $attachment->where_related_course('id', $course->id)->get();
        $user = new User();
        $user->where_related_course('id', $course->id)->get();
        $teacher = new User();
        $teacher->where_related_course('id', $course->id)->where_related_group('name', 'teachers')->get();
        $article = new Article();
        $article->where_related_course('id', $course->id)
                ->include_related('attachment')
                ->get();
        $data['template'] = 'user_area/course_single';
        if ($course->exists()) {
            $data['course'] = $course;
            $data['attachments'] = $attachment;
            $data['users'] = $user;
            $data['teacher'] = $teacher;
            $data['articles'] = $article;
            $this->load->view('template', $data);
        } else {
            redirect('errors/notFound');
        }
    }
    
    /**
     * 
     */
    public function create() {
        $course = new Course();
        if ($this->input->post('title')) {
            $course->title = $this->input->post('title');
             if ($course->save()) {
            echo 'success';
        } else {
            foreach ($course->error->all as $e) {
                echo $e . "<br />";
            }
        }
            
        }
    }
    
    
    /**
     * 
     */
    public function update($id = NULL) {
        $course = new Course();
        $course->where('id', $id)->get();
        if ($course->exists()) {
            if (isset($_GET['title']) && ($_GET['title'] != '')) { 
                $course->title = $_GET['title'];
            }
            $article->save();
        } else {
            echo 'Course does not exist';
        }
    }

    /**
     * 
     */
    public function delete($id = NULL) {
        $course = new Course();
        $course->where('id', $id)->get();
        if ($course->exists()) {
            $course->delete();
        } else {
            echo 'Course does not exist';
        }
    }
    
    public function myCourses() { 
        if ( !$this->ion_auth->logged_in() ) {
            redirect('/auth');
        } else {
            $classroom = new Classroom();
            $classroom->where_related_user('id', $this->ion_auth->user()->row()->id)
                    ->include_related('course')
                    ->include_related('course/category')
                    ->get();
            $data['template'] = 'user_area/courses_list';

            $data['classrooms'] = $classroom;
            $this->load->view('template', $data);

        }
    }

}
?>